﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace xmltest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            List<BasePower> basepower = new List<BasePower>();
            List<BaseVoltage> basevoltage = new List<BaseVoltage>();
            List<Connector> connector = new List<Connector>();
            List<CompositeSwitch> compositeswitch = new List<CompositeSwitch>();
            List<Bay> bay = new List<Bay>();
            List<Disconnector> disconnector = new List<Disconnector>();
            List<Switch>sswitch = new List<Switch>();
            List<Fuse> fuse = new List<Fuse>();
            List<GroundDisconnector> grounddisconnector = new List<GroundDisconnector>();
            List<LoadBreakSwitch> loadbreakswitch = new List<LoadBreakSwitch>();
            List<PowerTransformer> powertransformer = new List<PowerTransformer>();
            List<TransformerWinding> transformerwinding = new List<TransformerWinding>();
            List<BusbarSection> busbarsection = new List<BusbarSection>();
            List<Substation> substation = new List<Substation>();
            List<Breaker> breaker = new List<Breaker>();
            List<Circuit> circuit = new List<Circuit>();
            List<ACLineSegment> aclinesegment = new List<ACLineSegment>();
            List<Pole> pole = new List<Pole>();
            List<ConnectivityNode> connectivitynode = new List<ConnectivityNode>();
            List<Terminal> terminal = new List<Terminal>();
            List<Tnode> tnode = new List<Tnode>();
            List<string> havenread=new List<string>();
            List<string> noneed=new List<string>();           
            /*power和acline的表头*/
            ACLineSegment uu=new ACLineSegment();
            uu.head = "head";
            aclinesegment.Add(uu);
            PowerTransformer ooo=new PowerTransformer();
            ooo.head = "head";
            powertransformer.Add(ooo);

           /*读xml进内存*/
            XmlReader reader = XmlReader.Create(@"e://a/前锋线_3302693_2013-10-29.xml");
            while (!reader.EOF)
            {    reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {                
                    if (reader.Name.Equals("cim:CompositeSwitch"))
                    {
                        compositeswitch.Add(readCompositeSwitch(reader));
                    }
                    else if (reader.Name.Equals("cim:Connector"))
                    {
                        connector.Add(readConnector(reader));
                    }
                    else if (reader.Name.Equals("cim:Bay"))
                    {
                        bay.Add(readBay(reader));
                    }
                    else if (reader.Name.Equals("cim:Disconnector"))
                    {
                        disconnector.Add(readDisconnector(reader));
                    }
                    else if (reader.Name.Equals("cim:Fuse"))
                    {
                        fuse.Add(readFuse(reader));
                    }
                    else if (reader.Name.Equals("cim:GroundDisconnector"))
                    {
                        grounddisconnector.Add(readGroundDisconnector(reader));
                    }
                    else if (reader.Name.Equals("cim:LoadBreakSwitch"))
                    {
                        loadbreakswitch.Add(readLoadBreakSwitch(reader));
                    }
                    else if (reader.Name.Equals("cim:PowerTransformer"))
                    {
                        powertransformer.Add(readPowerTransformer(reader));
                    }
                    else if (reader.Name.Equals("cim:TransformerWinding"))
                    {
                        transformerwinding.Add(readTransformerWinding(reader));
                    }
                    else if (reader.Name.Equals("cim:BusbarSection"))
                    {
                        busbarsection.Add(readBusbarSection(reader));
                    }
                    else if (reader.Name.Equals("cim:BasePower"))
                    {
                        BasePower c = new BasePower();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                        reader.Read(); reader.Read(); reader.Read();
                        c.power = reader.ReadContentAsString();
                        basepower.Add(c);
                    }
                    else if (reader.Name.Equals("cim:BaseVoltage"))
                    {
                        BaseVoltage c = new BaseVoltage();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                        reader.Read(); reader.Read(); reader.Read();
                        c.voltage = reader.ReadContentAsString();
                        basevoltage.Add(c);
                    }
                    else if (reader.Name.Equals("cim:Substation"))
                    {
                        substation.Add(readSubstation(reader));
                    }
                    else if (reader.Name.Equals("cim:Breaker"))
                    {
                        breaker.Add(readBreaker(reader));
                    }
                    else if (reader.Name.Equals("cim:Circuit"))
                    {
                        circuit.Add(readCircuit(reader));
                    }
                    else if (reader.Name.Equals("cim:ACLineSegment"))
                    {
                        aclinesegment.Add(readACLineSegment(reader));
                    }
                    else if (reader.Name.Equals("cim:Pole"))
                    {
                        pole.Add(readPole(reader));
                    }
                    else if (reader.Name.Equals("cim:ConnectivityNode"))
                    {
                        ConnectivityNode c = new ConnectivityNode();
                        reader.MoveToAttribute("ID");
                        c.id = reader.ReadContentAsString();
                        connectivitynode.Add(c);
                    }
                    else if (reader.Name.Equals("cim:Terminal"))
                    {
                        terminal.Add(readTerminal(reader));
                    }
                    else if (reader.Name.Equals("cim:Switch"))
                    {
                        sswitch.Add(readSwitch(reader));
                    }

                }
            } reader.Close();
        /*把cn节点读入到aclinesegment等中*/       
            int j=0;
            int i=0 ;
            int ij=0; 
            foreach (Terminal c in terminal)
            {
                for (j = 1; j < aclinesegment.Count; j++)
                {
                    if (c.ceres.Equals(aclinesegment[j].id))
                    {
                        if (aclinesegment[j].cn1 == null)
                        { aclinesegment[j].cn1 = c.cnres; }
                        else aclinesegment[j].cn2 = c.cnres;
                        break;
                    }
                }                
                for (j = 0; j < connector.Count; j++)
               {
                   if (c.ceres.Equals(connector[j].id))
                   {
                       if (connector[j].cn1 == null)
                       { connector[j].cn1 = c.cnres; }
                       else if(connector[j].cn2==null)
                       { connector[j].cn2 = c.cnres;}
                       else
                       {
                           connector[j].cn3 = c.cnres;
                       }
                       break;
                   }                  
               }
                for (j = 0; j < disconnector.Count; j++)
                {
                    if (c.ceres.Equals(disconnector[j].id))
                    {
                        if (disconnector[j].cn1 == null)
                        { disconnector[j].cn1 = c.cnres; }
                        else disconnector[j].cn2 = c.cnres;
                        break;
                    }
                }
                for (j = 0; j < fuse.Count; j++)
                {
                    if (c.ceres.Equals(fuse[j].id))
                    {
                        if (fuse[j].cn1 == null)
                        { fuse[j].cn1 = c.cnres; }
                        else fuse[j].cn2 = c.cnres;
                        break;
                    }
                }
                for (j = 0; j < loadbreakswitch.Count; j++)
                {
                    if (c.ceres.Equals(loadbreakswitch[j].id))
                    {
                        if (loadbreakswitch[j].cn1 == null)
                        { loadbreakswitch[j].cn1 = c.cnres; }
                        else loadbreakswitch[j].cn2 = c.cnres;
                        break;
                    }
                }
                for (j = 0; j < grounddisconnector.Count; j++)
                {
                    if (c.ceres.Equals(grounddisconnector[j].id))
                    {
                        if (grounddisconnector[j].cn1 == null)
                        { grounddisconnector[j].cn1 = c.cnres; }
                        else grounddisconnector[j].cn2 = c.cnres;
                        break;
                    }
                }
                for (j = 1; j < powertransformer.Count; j++)
                {
                    if (c.ceres.Equals(powertransformer[j].id))
                    {
                        if (powertransformer[j].cn1 == null)
                        { powertransformer[j].cn1 = c.cnres; }
                        else powertransformer[j].cn2 = c.cnres;
                        break;
                    }
                }                
                for (j = 0; j < breaker.Count; j++)
                {
                    if (c.ceres.Equals(breaker[j].id))
                    {
                        if (breaker[j].cn1 == null)
                        { breaker[j].cn1 = c.cnres; }
                        else breaker[j].cn2 = c.cnres;
                        break;
                    }
                }
                for (j = 0; j < busbarsection.Count; j++)
                {
                    if (c.ceres.Equals(busbarsection[j].id))
                    {                        
                         busbarsection[j].cn1 = c.cnres; 
                        break;
                    }
                }
                for (j = 0; j < sswitch.Count; j++)
                {
                    if (c.ceres.Equals(sswitch[j].id))
                    {
                        if (sswitch[j].cn1 == null)
                        { sswitch[j].cn1 = c.cnres; }
                        else sswitch[j].cn2 = c.cnres;
                        break;
                    }
                }                                          
            }                   
    /*形成Tnode节点*/
            foreach (Breaker c in breaker)
            {
                havenread.Add(c.cn1);
                havenread.Add(c.cn2);
                if (c.normalopen=="1")
                {  
                   noneed.Add(c.cn1);
                   noneed.Add(c.cn2);
                }
                else
                {
                    Tnode d =new Tnode();
                    d.cn.Add(c.cn1);
                    d.cn.Add(c.cn2);
                    tnode.Add(d);
                }
            }
            foreach (LoadBreakSwitch c in loadbreakswitch)
            {
                havenread.Add(c.cn1);
                havenread.Add(c.cn2);
                if (c.normalopen =="1")
                {                    
                    noneed.Add(c.cn1);
                    noneed.Add(c.cn2);
                }
                else
                {
                    Tnode d = new Tnode();
                    d.cn.Add(c.cn1);
                    d.cn.Add(c.cn2);
                    tnode.Add(d);
                }
            }
            foreach (Switch c in sswitch)
            {
                havenread.Add(c.cn1);
                havenread.Add(c.cn2);
                if (c.normalopen == "1")
                {
                    noneed.Add(c.cn1);
                    noneed.Add(c.cn2);
                }
                else
                {
                    Tnode d = new Tnode();
                    d.cn.Add(c.cn1);
                    d.cn.Add(c.cn2);
                    tnode.Add(d);
                }
            }
            foreach (Disconnector c in disconnector)
            {
                havenread.Add(c.cn1);
                havenread.Add(c.cn2);
                if (c.normalopen == "1")
                {
                    noneed.Add(c.cn1);
                    noneed.Add(c.cn2);
                }
                else
                {
                    Tnode d = new Tnode();
                    d.cn.Add(c.cn1);
                    d.cn.Add(c.cn2);
                    tnode.Add(d);
                }
            }
            foreach (GroundDisconnector c in grounddisconnector)
            {
                havenread.Add(c.cn1);
                havenread.Add(c.cn2);
                if (c.normalopen == "1")
                {
                    noneed.Add(c.cn1);
                    noneed.Add(c.cn2);
                }
                else
                { 
                    Tnode d = new Tnode();
                    d.cn.Add(c.cn1);
                    d.cn.Add(c.cn2);
                    tnode.Add(d);
                }
            }
            foreach (Fuse c in fuse)
            {
                havenread.Add(c.cn1);
                havenread.Add(c.cn2);
                if (c.normalopen == "1")
                {
                    noneed.Add(c.cn1);
                    noneed.Add(c.cn2);
                }
                else
                {
                    Tnode d = new Tnode();
                    d.cn.Add(c.cn1);
                    d.cn.Add(c.cn2);
                    tnode.Add(d);
                }
             }         
            foreach (Connector c in connector)   /*节点合并法初步形成拓扑节点*/
            {
                if (c.cn1 != null && c.cn2 != null)
                {
                    if ((!(havenread.Contains(c.cn1))) && (!(havenread.Contains(c.cn2))))
                    {                      
                        Tnode d = new Tnode();
                        d.cn.Add(c.cn1);
                        d.cn.Add(c.cn2);
                        tnode.Add(d);
                        havenread.Add(c.cn1);
                        havenread.Add(c.cn2);                        
                    }
                    else if (havenread.Contains(c.cn1) && (!(havenread.Contains(c.cn2))))
                    {                     
                        if (!(noneed.Contains(c.cn1)))
                        {                                                       
                           foreach (Tnode p in tnode)
                            {
                                if (p.cn.Contains(c.cn1))
                                {
                                    p.cn.Add(c.cn2);
                                   if (c.cn3 != null) { p.cn.Add(c.cn3); havenread.Add(c.cn3);}
                                    break;
                                }                               
                            }                            
                            havenread.Add(c.cn2);
                        }                                            
                    }
                    else if (havenread.Contains(c.cn2) && (!(havenread.Contains(c.cn1))))
                    {                       
                        if (!(noneed.Contains(c.cn2)))
                        {                                                      
                            foreach (Tnode p in tnode)
                            {
                                if (p.cn.Contains(c.cn2))
                                {
                                   p.cn.Add(c.cn1);
                                   if (c.cn3 != null) { p.cn.Add(c.cn3); havenread.Add(c.cn3); }
                                   
                                    break;
                                }                
                            }                           
                            havenread.Add(c.cn1);
                        }                        
                    }
                    else if (havenread.Contains(c.cn1) && havenread.Contains(c.cn2))
                    {
                        if ((!(noneed.Contains(c.cn1))) && (!(noneed.Contains(c.cn2))))
                        {
                            i = -1;
                            j = -1;                          
                            foreach (Tnode k in tnode)
                            {
                                if (k.cn.Contains(c.cn1))
                                {
                                    i = tnode.IndexOf(k);
                                    break;
                                }
                            }
                            foreach (Tnode k in tnode)
                            {
                                if (k.cn.Contains(c.cn2))
                                {
                                    j = tnode.IndexOf(k);
                                    break;
                                }
                            }
                            if (i!=-1&&j!=-1)
                            {
                                if (i != j)
                                {
                                    if (i > j)
                                    {
                                        ij = i;
                                        i = j;
                                        j = ij;
                                    }
                                    for (int h = 0; h < tnode[j].cn.Count; h++)
                                    {
                                        tnode[i].cn.Add(tnode[j].cn[h]);
                                    }
                                    tnode.RemoveAt(j);
                                }
                             }
                            else if(i==-1&&j!=-1)
                            {
                                tnode[j].cn.Add(c.cn1);
                            }
                            else if (i != -1 && j == -1)
                            {
                                tnode[i].cn.Add(c.cn2);
                            }
                        }                   
                       }
                     }
                else if(c.cn2==null)
                {
                    havenread.Add(c.cn1);
                }
            }
  /*电缆没有T的一端自己成为T*/
            List<Tnode> xintnode = new List<Tnode>();
            foreach (Tnode ff in tnode)
            {
                xintnode.Add(ff);
            }
            foreach (ACLineSegment dd in aclinesegment)
            {
                if (dd.head == null)
                {
                    if (dd.model.Equals("\n    "))
                    {
                        dd.model = null;
                    }
                    if (dd.length.Equals("0.000"))
                    {
                        dd.length = "0.01";
                    }                  
                    if (!(dd.cn1==null && dd.cn2 ==null))
                    {
                        i = -1;
                        j = -1;                    
                        foreach (Tnode d in tnode)
                        {
                            if (d.cn.Contains(dd.cn1))
                            {
                                i = tnode.IndexOf(d);
                            }

                            if (d.cn.Contains(dd.cn2))
                            {
                                j = tnode.IndexOf(d);                         
                            }
                        }
                        if (i == -1 && j != -1)
                        {
                            if (!havenread.Contains(dd.cn1) || dd.cn1 == null)
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);
                                havenread.Add(dd.cn1);
                            }
                            else if(noneed.Contains(dd.cn1))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);
                            }
                        }                       
                        else if (i != -1 && j == -1)
                        {
                            if (!havenread.Contains(dd.cn2)||dd.cn2==null)
                            {
                            Tnode hh = new Tnode();
                            hh.cn.Add(dd.cn2);
                            xintnode.Add(hh);
                            havenread.Add(dd.cn2);
                            }
                            else if (noneed.Contains(dd.cn2))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn2);
                                xintnode.Add(hh);
                            }
                        }
                        else if (i == -1 && j == -1)
                        {
                            if (havenread.Contains(dd.cn1) && !havenread.Contains(dd.cn2)&&!noneed.Contains(dd.cn1))
                            {
                                Tnode h = new Tnode();
                                h.cn.Add(dd.cn2);
                                xintnode.Add(h);
                                havenread.Add(dd.cn2);
                            }
                            else if (havenread.Contains(dd.cn1) && !havenread.Contains(dd.cn2) && noneed.Contains(dd.cn1))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);
                                Tnode h = new Tnode();
                                h.cn.Add(dd.cn2);
                                xintnode.Add(h);
                                havenread.Add(dd.cn2);
                            }
                            else if (!havenread.Contains(dd.cn1) && havenread.Contains(dd.cn2) && !noneed.Contains(dd.cn2))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);
                                havenread.Add(dd.cn1);
                            }
                            else if (!havenread.Contains(dd.cn1) && havenread.Contains(dd.cn2) && noneed.Contains(dd.cn2))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);
                                Tnode h = new Tnode();
                                h.cn.Add(dd.cn2);
                                xintnode.Add(h);
                                havenread.Add(dd.cn1);
                            }
                            else if (!havenread.Contains(dd.cn1) && !havenread.Contains(dd.cn2))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);
                                Tnode h = new Tnode();
                                h.cn.Add(dd.cn2);
                                xintnode.Add(h);
                                havenread.Add(dd.cn1);
                                havenread.Add(dd.cn2);
                            }
                            else if (havenread.Contains(dd.cn1) && havenread.Contains(dd.cn2) && noneed.Contains(dd.cn1))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn1);
                                xintnode.Add(hh);                             
                            }
                            else if (havenread.Contains(dd.cn1) && havenread.Contains(dd.cn2) && noneed.Contains(dd.cn2))
                            {
                                Tnode hh = new Tnode();
                                hh.cn.Add(dd.cn2);
                                xintnode.Add(hh);
                            }
                        }                       
                    }
                }                
            }                    
            /*变压器有一端没连有T的自己成为一个T*/     
            foreach (PowerTransformer dd in powertransformer)
            {
                if (dd.head == null)
                {
                    if (dd.model.Equals("\n    "))
                    {
                        dd.model = null;
                    }
                    if (dd.ratedMVA.Equals("0"))
                    {
                        dd.ratedMVA = "500";
                    }
                    i = -1;
                    j = -1;                  
                    foreach (Tnode d in tnode)
                    {
                        if (d.cn.Contains(dd.cn1))
                        {
                            i = tnode.IndexOf(d);
                        }

                        if (d.cn.Contains(dd.cn2))
                        {
                            j = tnode.IndexOf(d);                            
                        }
                    }
                    if (i == -1 && j != -1)
                    {
                        Tnode hh = new Tnode();
                        hh.cn.Add(dd.cn1);
                        xintnode.Add(hh);
                    }
                    else if (i != -1 && j == -1)
                    {
                        Tnode hh = new Tnode();
                        hh.cn.Add(dd.cn2);
                        xintnode.Add(hh);
                    }
                    else if (i == -1 && j == -1)
                    {
                        Tnode hh = new Tnode();
                        hh.cn.Add(dd.cn1);
                        xintnode.Add(hh);
                        Tnode h = new Tnode();
                        h.cn.Add(dd.cn2);
                        xintnode.Add(h);
                    }
                }
            }         
            List<Tnode> bus =new List<Tnode>();
            List<int> nn=new List<int>();
            List<ACLineSegment> line =new List<ACLineSegment>();
            List<PowerTransformer> xinpower=new List<PowerTransformer>();
            int mm;
       /*bus表头*/      
            Tnode pp=new Tnode();
                pp.cn.Add(null);
                bus.Add(pp);                
     /*去掉两端都没连接有东西的ACline形成新的line*/
                foreach (ACLineSegment yy in aclinesegment)
                {
                    if (yy.head != null)
                    {
                        line.Add(yy);
                    }
                    else if (yy.head == null && (yy.cn1 != null || yy.cn2 != null))
                    {
                        line.Add(yy);
                    }
                }
                for (int k = 1; k < line.Count; k++)/*去掉两端都连断开开关的线路*/
                {
                    if (noneed.Contains(line[k].cn1) && noneed.Contains(line[k].cn2))
                    {
                        line.RemoveAt(k);
                        k = k - 1;
                    }
                }
        /*拓扑节点编号*/
            foreach (ACLineSegment kk in line)
            {
                if (kk.head == null)
                { 
                    foreach (Tnode d in xintnode)
                    {
                        if (d.cn.Contains(kk.cn1) || d.cn.Contains(kk.cn2))
                        {
                            if (!(nn.Contains(xintnode.IndexOf(d))))
                            {
                                bus.Add(d);                              
                                i = xintnode.IndexOf(d);
                                nn.Add(i);
                            }
                        }
                    } 
                }
            }
            foreach (PowerTransformer k in powertransformer)
            {   xinpower.Add(k);
                if (k.head == null)
                {
                    foreach (Tnode d in xintnode)
                    {
                        if (d.cn.Contains(k.cn1) || d.cn.Contains(k.cn2))
                        {
                            if (!(nn.Contains(xintnode.IndexOf(d))))
                            {
                                bus.Add(d);
                                i = xintnode.IndexOf(d);
                                nn.Add(i);
                            }
                        }
                    }
                }
            }   
            /*电缆初步连母线*/
            foreach (ACLineSegment k in line)
            {
                if (k.head == null)
                {
                    if (!(k.cn1 == null && k.cn2 == null))
                    {
                        for (int l = 1; l < bus.Count; l++)
                        {
                            if (bus[l].cn.Contains(k.cn1))
                            {
                                mm = l;
                                if (k.link1.Equals(0))
                                {
                                    k.link1 = mm;
                                }
                                else
                                {
                                    k.link2 = mm;
                                }
                            }
                            else if (bus[l].cn.Contains(k.cn2))
                            {
                                mm = l;
                                if (k.link1.Equals(0))
                                {
                                    k.link1 = mm;
                                }
                                else
                                {
                                    k.link2 = mm;
                                }
                            }
                        }
                    }
                }
            }
            /*变压器初步连接母线*/         
            foreach (PowerTransformer k in xinpower)
            {
                if (k.head == null)
                {
                    for (int l = 1; l <bus.Count ; l++)
                    {                                                                 
                        if (bus[l].cn.Contains(k.cn1))
                        {
                            mm = l;                       
                            if (k.link1.Equals(0)) 
                            {
                                k.link1 = mm;                                                                                     
                            }
                            else
                            {
                                k.link2 = mm;                               
                            }                                                       
                        }
                        else if (bus[l].cn.Contains(k.cn2))
                        {
                            mm = l;                       
                            if (k.link1.Equals(0))
                            {
                                k.link1 = mm;                             
                            }
                            else
                            {
                                k.link2 = mm;                              
                            }                         
                        }
                    }
                }
            }          
            /*拓扑岛分析*/
            List<Tpudao>tpudao=new List<Tpudao>();
               List<int>n1=new List<int>();
            foreach (var oi in line)
            {
                if (oi.head == null)
                {    if (!n1.Contains(oi.link1) && !n1.Contains(oi.link2))
                    {
                        Tpudao kj=new Tpudao();
                        kj.cn.Add(oi.id);
                        kj.lk.Add(oi.link1);
                        kj.lk.Add(oi.link2);
                        tpudao.Add(kj);
                        n1.Add(oi.link1);
                        n1.Add(oi.link2);
                     }
                    else if (n1.Contains(oi.link1) && !n1.Contains(oi.link2))
                    {
                        foreach (var gf in tpudao)
                        {
                            if (gf.lk.Contains(oi.link1))
                            {
                                gf.cn.Add(oi.id);
                                gf.lk.Add(oi.link2);
                                break;
                            }
                        }
                        n1.Add(oi.link2);
                    }
                    else if (!n1.Contains(oi.link1) && n1.Contains(oi.link2))
                    {
                        foreach (var gf in tpudao)
                        {
                            if (gf.lk.Contains(oi.link2))
                            {
                                gf.cn.Add(oi.id);
                                gf.lk.Add(oi.link1);
                                break;
                            }
                        }
                        n1.Add(oi.link1);
                    }               
                    else if (n1.Contains(oi.link1) && n1.Contains(oi.link2))
                    {
                        i = 0;
                        j = 0;
                        foreach (Tpudao k in tpudao)
                        {
                            if (k.lk.Contains(oi.link1))
                            {
                                i = tpudao.IndexOf(k);
                                k.cn.Add(oi.id);                               
                                break;
                            }
                        }
                        foreach (Tpudao k in tpudao)
                        {
                            if (k.lk.Contains(oi.link2))
                            {
                                j = tpudao.IndexOf(k);
                                break;
                            }
                        }
                        if (i != j)
                        {
                            if (i > j)
                            {
                                ij = i;
                                i = j;
                                j = ij;
                            }
                            for (int h = 0; h < tpudao[j].cn.Count; h++)
                            {
                                tpudao[i].cn.Add(tpudao[j].cn[h]);
                            }
                            for (int h = 0; h < tpudao[j].lk.Count; h++)
                            {
                                tpudao[i].lk.Add(tpudao[j].lk[h]);
                            }
                            tpudao.RemoveAt(j);
                        }                                              
                    }
                }
            }
            foreach (var oi in xinpower)
            {
                if (oi.head == null)
                {
                    if (!n1.Contains(oi.link1) && !n1.Contains(oi.link2))
                    {
                        Tpudao kj = new Tpudao();
                        kj.cn.Add(oi.id);
                        kj.lk.Add(oi.link1);
                        kj.lk.Add(oi.link2);
                        tpudao.Add(kj);
                        n1.Add(oi.link1);
                        n1.Add(oi.link2);
                    }
                    else if (n1.Contains(oi.link1) && !n1.Contains(oi.link2))
                    {
                        foreach (var gf in tpudao)
                        {
                            if (gf.lk.Contains(oi.link1))
                            {
                                gf.cn.Add(oi.id);
                                gf.lk.Add(oi.link2);
                                break;
                            }
                        }
                        n1.Add(oi.link2);
                    }
                    else if (!n1.Contains(oi.link1) && n1.Contains(oi.link2))
                    {
                        foreach (var gf in tpudao)
                        {
                            if (gf.lk.Contains(oi.link2))
                            {
                                gf.cn.Add(oi.id);
                                gf.lk.Add(oi.link1);
                                break;
                            }
                        }
                        n1.Add(oi.link1);
                    }
                    else if (n1.Contains(oi.link1) && n1.Contains(oi.link2))
                    {
                        i = 0;
                        j = 0;
                        foreach (Tpudao k in tpudao)
                        {
                            if (k.lk.Contains(oi.link1))
                            {
                                i = tpudao.IndexOf(k);
                                k.cn.Add(oi.id);     
                                break;
                            }
                        }
                        foreach (Tpudao k in tpudao)
                        {
                            if (k.lk.Contains(oi.link2))
                            {
                                j = tpudao.IndexOf(k);
                                break;
                            }
                        }
                        if (i != j)
                        {
                            if (i > j)
                            {
                                ij = i;
                                i = j;
                                j = ij;
                            }
                            for (int h = 0; h < tpudao[j].cn.Count; h++)
                            {
                                tpudao[i].cn.Add(tpudao[j].cn[h]);
                            }
                            for (int h = 0; h < tpudao[j].lk.Count; h++)
                            {
                                tpudao[i].lk.Add(tpudao[j].lk[h]);
                            }
                            tpudao.RemoveAt(j);
                        }
                    }
                }
            }
            j = 0;
            for (int k = 0; k < tpudao.Count; k++)/*确定连接最多线路和变压器的拓扑岛*/
            {    
                if (tpudao[k].cn.Count>j)
                {
                    j = tpudao[k].cn.Count;
                    i = k;
                }
            }      
            for (int k = 0; k < tpudao.Count; k++)/*留下含线路和变压器最多的拓扑岛*/
            {
                if (k!=i)
                {
                    for (int l = 1; l < line.Count; l++)
                    {
                        if (tpudao[k].cn.Contains(line[l].id))
                        {
                           
                            for (int m = 1; m < bus.Count; m++)
                            {
                                if (bus[m].cn.Contains(line[l].cn1) || bus[m].cn.Contains(line[l].cn2))
                                {
                                    bus.RemoveAt(m);
                                    m = m - 1;
                                }
                            }
                            line.RemoveAt(l);
                            l = l - 1;
                        }
                    }
                    for (int l = 1; l < xinpower.Count; l++)
                    {
                        if (tpudao[k].cn.Contains(xinpower[l].id))
                        {
                          
                            for (int m = 1; m < bus.Count; m++)
                            {
                                if (bus[m].cn.Contains(xinpower[l].cn1) || bus[m].cn.Contains(xinpower[l].cn2))
                                {
                                    bus.RemoveAt(m);
                                    m = m - 1;
                                }
                            }
                            xinpower.RemoveAt(l);
                            l = l - 1;
                        }
                    }
                }
            }
            foreach (var tg in line)/*连接点清零*/
            {
                if (tg.head == null)
                {
                    tg.link1 = 0; tg.link2 = 0;                 
                }
            }
            foreach (var tg in xinpower)
            {
                if (tg.head == null)
                {
                    tg.link1 = 0; tg.link2 = 0;
                }
            }          
            /*电缆连母线*/
            foreach (ACLineSegment k in line)
            {
                if (k.head == null)
                {
                    if (!(k.cn1 == null && k.cn2 == null))
                    {
                        for (int l = 1; l < bus.Count; l++)
                        {
                            if (bus[l].cn.Contains(k.cn1))
                            {
                                mm = l;
                                if (k.link1.Equals(0))
                                {
                                    k.link1 = mm;
                                }
                                else
                                {
                                    k.link2 = mm;
                                }
                            }
                            else if (bus[l].cn.Contains(k.cn2))
                            {
                                mm = l;
                                if (k.link1.Equals(0))
                                {
                                    k.link1 = mm;
                                }
                                else
                                {
                                    k.link2 = mm;
                                }
                            }
                        }
                    }
                }
            }
            /*变压器连接母线*/
            foreach (PowerTransformer k in xinpower)
            {
                if (k.head == null)
                {
                    for (int l = 1; l < bus.Count; l++)
                    {
                        if (bus[l].cn.Contains(k.cn1))
                        {
                            mm = l;
                            if (k.link1.Equals(0))
                            {
                                k.link1 = mm;
                            }
                            else
                            {
                                k.link2 = mm;
                            }
                        }
                        else if (bus[l].cn.Contains(k.cn2))
                        {
                            mm = l;
                            if (k.link1.Equals(0))
                            {
                                k.link1 = mm;
                            }
                            else
                            {
                                k.link2 = mm;
                            }
                        }
                    }
                }
            }
            foreach (var tt in xinpower)   /*标记几个变压器连着同一母线的情况*/
            {
                if (tt.head == null &&tt.yidu ==1)
                {
                    foreach (var ee in xinpower)
                    {
                        if (ee.link1.Equals(tt.link1))
                        {
                            ee.yidu = 1;
                            break;
                        }
                    }
                }
            }
            /*分解line的型号*/            
            List<string[]> chafen = new List<string[]>();
            List<string[]>  modelcha=new List<string[]>();            
            /*读入LineType文件*/
             string filePath = "e:\\LineType.txt";
           List<string[]> lineread = Readtxtline(filePath);   //这个函数把文件的每一行读入lineread
            double R = 0.3;
            double X = 0.2;
            double UN = 10.5;
            double SB = 1;
            double UB = 1.05;                   
            foreach (var bb in lineread)
            {                   
                string[] sstrr = bb[1].Split(new string[] { "22", "-" }, StringSplitOptions.RemoveEmptyEntries);
                chafen.Add(sstrr);
            }
            foreach (var ee in line)
            {
                if (ee.head==null&&ee.model!=null)
                {   
                    string[] sttr = ee.model.Split(new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
                    modelcha.Add(sttr);
                }
            }
            foreach (ACLineSegment k in line)
            {
                if (k.head==null)
                {
                    if (k.model==null||k.model.Equals("不详"))
                    {
                        k.r = (double.Parse(k.length)*R)*SB/(UB*UB);
                        k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                        k.x = (double.Parse(k.length) * X) * SB / (UB * UB);
                        k.x = Math.Round(k.x, 10);
                        k.b = 0.0;
                    }
                    foreach (var g in lineread)
                    {
                        if (k.model != null)
                        {
                            if (k.model.Equals(g[1]))
                            {
                                k.r = (double.Parse(g[2])*double.Parse(k.length))*SB/(UB*UB);
                                k.r = Math.Round(k.r, 10);
                                k.x = (X*double.Parse(k.length))*SB/(UB*UB);
                                k.x = Math.Round(k.x, 10);
                                k.b = 0.0; 
                            }
                        }
                    }
                    if (k.r.Equals(0.0)&&k.x.Equals(0.0))
                    {
                        foreach (var ww in modelcha)
                        {
                            foreach (var qq in chafen)
                            {
                                if (qq.Length.Equals(2)&&qq[0].Equals(ww[0])&&qq[1].Equals(ww[1]))
                                {
                                    k.r = (double.Parse(lineread[chafen.IndexOf(qq)][2]) * double.Parse(k.length)) * SB / (UB * UB);
                                    k.r = Math.Round(k.r, 10);
                                    k.x = (X * double.Parse(k.length)) * SB / (UB * UB);
                                    k.x = Math.Round(k.x, 10);
                                    k.b = 0.0;
                                }
                                else if (qq.Length.Equals(3) && qq[0].Equals(ww[0]) && (qq[1].Equals(ww[1])||qq[2].Equals(ww[1])))
                                {
                                    k.r = (double.Parse(lineread[chafen.IndexOf(qq)][2]) * double.Parse(k.length)) * SB / (UB * UB);
                                    k.r = Math.Round(k.r, 10);
                                    k.x = (X * double.Parse(k.length)) * SB / (UB * UB);
                                    k.x = Math.Round(k.x, 10);
                                    k.b = 0.0;
                                }
                            }
                        }
                    }  
                                     
                }
            }
            foreach (ACLineSegment bb in line)
            {
                if (bb.head == null)
                { 
                    int index = line.IndexOf(bb);
                    Console.WriteLine(index + " " +bb.id + " " + bb.link1 + " " + bb.link2);
                }
            }                                      
            /*读trans文件*/
            string filesPath = "e:\\trans.csv";
            List<string[]> transread = Readtrans(filesPath);
            double P0 = 2.21;
            double PK = 3.38;
            double I0 = 1.12; 
            double UK = 4.0;
            foreach (var k in xinpower)
            {
                if (k.head == null)
                {
                    if (k.model == null)
                    {
                        if (double.Parse(k.ratedMVA) <= 100)
                        {
                            k.r = (2.25*UN*UN/(double.Parse(k.ratedMVA)*double.Parse(k.ratedMVA)))*SB/(UB*UB);
                            k.r = Math.Round(k.r, 10); //保留小数点后10位数
                            k.x = (3*UN*UN/(100*double.Parse(k.ratedMVA)))*SB/(UB*UB);
                            k.x = Math.Round(k.x, 10);
                            k.g = (0.3/(1000*UN*UN))*SB/(UB*UB);
                            k.g = Math.Round(k.g, 10);
                            k.b = (1.3*double.Parse(k.ratedMVA)/(100*1000*UN*UN))*SB/(UB*UB);
                            k.b = Math.Round(k.b, 10);
                        }
                        else
                        {
                            k.r = (PK*UN*UN*1000/(double.Parse(k.ratedMVA)*double.Parse(k.ratedMVA)))*SB/(UB*UB);
                            k.r = Math.Round(k.r, 10); //保留小数点后10位数
                            k.x = (10*UK*UN*UN/double.Parse(k.ratedMVA))*SB/(UB*UB);
                            k.x = Math.Round(k.x, 10);
                            k.g = (P0/(1000*UN*UN))*SB/(UB*UB);
                            k.g = Math.Round(k.g, 10);
                            k.b = (I0*double.Parse(k.ratedMVA)/(100*1000*UN*UN))*SB/(UB*UB);
                            k.b = Math.Round(k.b, 10);
                        }
                    }
                    foreach (var g in transread)
                    {
                        if (k.model != null)
                        {
                            if (k.model.Equals(g[0]))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10); 
                            }
                            else if (k.model.Substring(k.model.Length - 2, 2).Equals(".5") && k.model.Substring(0, k.model.Length - 2).Equals(g[0]))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10); 
                            }
                            else if (k.model.Length>=4&&k.model.Substring(k.model.Length - 4, 4).Equals("-0.4") && k.model.Substring(0, k.model.Length - 4).Equals(g[0]))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10); 
                            }
                            else if (g[0].Substring(g[0].Length - 2, 2).Equals(".5") && g[0].Substring(0, g[0].Length - 2).Equals(k.model))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10); 
                            }
                            else if (g[0].Substring(g[0].Length - 3, 3).Equals("/10") && g[0].Substring(0, g[0].Length - 3).Equals(k.model))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10);
                            }
                            else if (g[0].Substring(g[0].Length - 4, 4).Equals("/10.5") && g[0].Substring(0, g[0].Length - 4).Equals(k.model))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10);
                            }
                            else if (k.model.Length >= 3 && k.model.Substring(k.model.Length - 3, 3).Equals("kVA") && g[0].Substring(0, g[0].Length - 4).Equals(k.model.Substring(0, k.model.Length - 3)))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10);
                            }
                            else if (k.model.Length >= 3 && k.model.Substring(k.model.Length - 3, 3).Equals("kVA") && g[0].Equals(k.model.Substring(0, k.model.Length - 3)))
                            {
                                k.r = (double.Parse(g[2]) * UN * UN * 1000 / (double.Parse(k.ratedMVA) * double.Parse(k.ratedMVA))) * SB / (UB * UB);
                                k.r = Math.Round(k.r, 10);                             //保留小数点后10位数
                                k.x = (10 * double.Parse(g[4]) * UN * UN / double.Parse(k.ratedMVA)) * SB / (UB * UB);
                                k.x = Math.Round(k.x, 10);
                                k.g = (double.Parse(g[1]) / (1000 * UN * UN)) * SB / (UB * UB);
                                k.g = Math.Round(k.g, 10);
                                k.b = (double.Parse(g[3]) * double.Parse(k.ratedMVA) / (100 * 1000 * UN * UN)) * SB / (UB * UB);
                                k.b = Math.Round(k.b, 10);
                            }                          
                        }                       
                    }
                    if (k.r.Equals(0.0) && k.x.Equals(0.0))
                    {
                        if (double.Parse(k.ratedMVA) <= 100)
                        {   
                            k.r = (2.25*UN*UN/(double.Parse(k.ratedMVA)*double.Parse(k.ratedMVA)))*SB/(UB*UB);
                            k.r = Math.Round(k.r, 10); //保留小数点后10位数
                            k.x = (3*UN*UN/(100*double.Parse(k.ratedMVA)))*SB/(UB*UB);
                            k.x = Math.Round(k.x, 10);
                            k.g = (0.3/(1000*UN*UN))*SB/(UB*UB);
                            k.g = Math.Round(k.g, 10);
                            k.b = (1.3*double.Parse(k.ratedMVA)/(100*1000*UN*UN))*SB/(UB*UB);
                            k.b = Math.Round(k.b, 10);
                        }
                        else
                        {
                            k.r = (PK*UN*UN*1000/(double.Parse(k.ratedMVA)*double.Parse(k.ratedMVA)))*SB/(UB*UB);
                            k.r = Math.Round(k.r, 10); //保留小数点后10位数
                            k.x = (10*UK*UN*UN/double.Parse(k.ratedMVA))*SB/(UB*UB);
                            k.x = Math.Round(k.x, 10);
                            k.g = (P0/(1000*UN*UN))*SB/(UB*UB);
                            k.g = Math.Round(k.g, 10);
                            k.b = (I0*double.Parse(k.ratedMVA)/(100*1000*UN*UN))*SB/(UB*UB);
                            k.b = Math.Round(k.b, 10);
                        }
                    }

                }
            }
            foreach (var bb in xinpower)
            {
                if (bb.head == null)
                {
                    int index = xinpower.IndexOf(bb);
                    Console.WriteLine(index+" "+bb.name + " " +bb.link1+" "+bb.link2);
                }
            }                    
            /*读负荷文件*/
            double pinghe=0.0;
            string sstr = "e:\\lz.csv";
            List<string[]> fuheread = Readfuhe(sstr);
            foreach (var po in xinpower)
            {
                if (po.head == null)
                {
                    foreach (var ko in fuheread)
                    {
                        if (ko[0].Equals(po.id))
                        {
                            po.Pli = (double.Parse(ko[1]) + double.Parse(ko[2]) + double.Parse(ko[3]))/
                                     (double.Parse(basepower[0].power)*1000);
                            po.Qli = (double.Parse(ko[4]) + double.Parse(ko[5]) + double.Parse(ko[6]))/
                                     (double.Parse(basepower[0].power)*1000);
                        }
                        else
                        {
                            po.Pli = -1;
                            po.Qli = -1;
                        }
                    }
                    /*确定平衡节点*/
                    if (po.name.IndexOf("#1") > 0)
                    {
                        pinghe = po.link1;
                    }
                    else
                    {
                        if (po.name.IndexOf("#2") > 0)
                        {
                            pinghe = po.link1;
                        }
                        else
                        {
                            pinghe = bus.Count - 2;
                        }                       
                    }
                }
            }        
            /*输出IEEE格式数据*/
            List<Tnode>  ieee=new List<Tnode>();
            Tnode a =new Tnode();
            a.cn.Add((bus.Count-1).ToString());//节点数
            a.cn.Add((line.Count+xinpower.Count-2).ToString());//支路数和变压器总数
            a.cn.Add("100");//基准容量
            a.cn.Add("1e-5");//迭代精度
            a.cn.Add("0");//目标函数类型
            ieee.Add(a);
            Tnode b =new Tnode();        
            b.cn.Add("0");
            ieee.Add(b);
            Tnode cc=new Tnode();
            cc.cn.Add("1");//序号
            cc.cn.Add(pinghe.ToString());//平衡节点
            cc.cn.Add("1");//平衡节点电压
            ieee.Add(cc);
            Tnode a1=new Tnode();
            a1.cn.Add("0");
            ieee.Add(a1);
            i = 1;
            foreach (var ff in line)/*输出线路模块数据*/
            {
                if (ff.head == null)
                {
                    Tnode ww = new Tnode();
                    ww.cn.Add(i.ToString()); //序号
                    i = i + 1;
                    ww.cn.Add(ff.link1.ToString());
                    ww.cn.Add(ff.link2.ToString());
                    ww.cn.Add(ff.r.ToString("F10"));
                    ww.cn.Add(ff.x.ToString("F10"));
                    ww.cn.Add("0");
                    ww.cn.Add("0");
                    ieee.Add(ww);
                }
            }           
            Tnode a2 = new Tnode();
            a2.cn.Add("0");
            ieee.Add(a2);           
            j = 1;
            foreach (var jj in xinpower)/*输出变压器模块数据*/
            {
                if (jj.head == null)
                {
                    Tnode sd = new Tnode();
                    sd.cn.Add(j.ToString());
                    j = j + 1;
                    sd.cn.Add("0");
                    sd.cn.Add(jj.link1.ToString());
                    sd.cn.Add(jj.link2.ToString());
                    sd.cn.Add(jj.r.ToString("F10"));
                    sd.cn.Add(jj.x.ToString("F10"));
                    sd.cn.Add("1");
                    sd.cn.Add("1");
                    sd.cn.Add("1");
                    sd.cn.Add("1");
                    ieee.Add(sd);
                }
            }
            Tnode a4 = new Tnode();a4.cn.Add("0"); ieee.Add(a4);           
            Tnode u1=new Tnode();
            u1.cn.Add("1"); u1.cn.Add("0"); u1.cn.Add("0"); u1.cn.Add("0");
            u1.cn.Add("0"); u1.cn.Add("0"); u1.cn.Add("0"); u1.cn.Add("0");
            u1.cn.Add("0"); u1.cn.Add("0"); u1.cn.Add("0"); u1.cn.Add("0");
            ieee.Add(u1);
            Tnode e4 = new Tnode(); e4.cn.Add("0"); ieee.Add(e4);  
            i = 1;
            foreach (var rr in xinpower)/*输出接地支路数据*/
            {
                if (rr.head == null)
                {
                    Tnode c1 = new Tnode();
                    c1.cn.Add(i.ToString()); //序号
                    i = i + 1;                  
                    c1.cn.Add(rr.link1.ToString());
                    c1.cn.Add(rr.g.ToString("F10"));
                    c1.cn.Add(rr.b.ToString("F10"));
                    ieee.Add(c1);
                }
            }
            Tnode a5 = new Tnode(); a5.cn.Add("0"); ieee.Add(a5);
            Tnode a6 = new Tnode(); a6.cn.Add("0"); ieee.Add(a6);                   
            Tnode a7 = new Tnode();
            a7.cn.Add("0"); ieee.Add(a7);                      
            i = 1;
            for (int k = 1; k < bus.Count; k++)/*输出节点参数数据*/
            {
                Tnode qw=new Tnode();
                qw.cn.Add(i.ToString()); //序号         
                qw.cn.Add("-1");
                qw.cn.Add(i.ToString()); //节点号
                qw.cn.Add("10");//电压等级
                qw.cn.Add("0");
                qw.cn.Add("0");
                foreach (var er in xinpower)
                {
                    if (er.head == null)
                    {
                        if (er.yidu != 1&&er.link1 == i)
                        {                   
                                if (er.Pli != -1)
                                {
                                    qw.cn.Add(er.Pli.ToString("F10"));
                                    qw.cn.Add(er.Qli.ToString("F10"));
                                }
                                else
                                {
                                    qw.cn.Add(er.Pli.ToString());
                                    qw.cn.Add(er.Qli.ToString());
                                }
                                j = -1;                      
                        }
                        else if (er.yidu == 1 && er.link2 == i)
                        {
                            if (er.Pli != -1)
                            {
                                qw.cn.Add(er.Pli.ToString("F10"));
                                qw.cn.Add(er.Qli.ToString("F10"));
                            }
                            else
                            {
                                qw.cn.Add(er.Pli.ToString());
                                qw.cn.Add(er.Qli.ToString());
                            }
                            j = -1;               
                        }
                    }
                }
                if (j != -1)
                {
                    qw.cn.Add("0");
                    qw.cn.Add("0");
                }
                qw.cn.Add("0");
                qw.cn.Add("0");
                ieee.Add(qw);
                i = i + 1;
            }
            Tnode b1= new Tnode();
            b1.cn.Add("0");
            ieee.Add(b1);    
            Tnode b2=new Tnode();
            b2.cn.Add("1"); b2.cn.Add("1"); b2.cn.Add(pinghe.ToString());
            b2.cn.Add("1");
            b2.cn.Add("-100");
            b2.cn.Add("100");
            b2.cn.Add("0"); b2.cn.Add("0");
            b2.cn.Add("0"); b2.cn.Add("0");
            ieee.Add(b2);
            Tnode b3 = new Tnode();
            b3.cn.Add("0");
            ieee.Add(b3);
            Tnode b4 = new Tnode();
            b4.cn.Add("0");
            ieee.Add(b4);
            Tnode c2 = new Tnode();
            c2.cn.Add("1"); c2.cn.Add("1"); c2.cn.Add(pinghe.ToString());
            c2.cn.Add("1");
            c2.cn.Add("-100");
            c2.cn.Add("100");
            ieee.Add(c2);
            Tnode c3 = new Tnode(); c3.cn.Add("0"); ieee.Add(c3);
            Tnode c4 = new Tnode(); c4.cn.Add("0"); ieee.Add(c4);
            Tnode c5 = new Tnode(); c5.cn.Add("1");
            c5.cn.Add(pinghe.ToString());c5.cn.Add("0.999");c5.cn.Add("1.001");
            ieee.Add(c5);
            Tnode d4 = new Tnode(); d4.cn.Add("0"); ieee.Add(d4);
            Tnode d1 = new Tnode(); d1.cn.Add("0"); ieee.Add(d1);
            Tnode d2 = new Tnode(); d2.cn.Add("0"); ieee.Add(d2);
            Tnode d3 = new Tnode(); d3.cn.Add("0"); ieee.Add(d3);                                      
            WriteInfoTofile("e:\\0.txt", ieee);



        }            
        /*reader 到达cim:CompositeSwitch等根部的XmlReader流。
         返回在reader当前位置的完整的CompositeSwitch等对象。*/
        private CompositeSwitch readCompositeSwitch(XmlReader reader)
        {
            CompositeSwitch c = new CompositeSwitch();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:CompositeSwitch")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Connector readConnector(XmlReader reader)
        {
            Connector c = new Connector();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Connector")))
            {

                if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }

                else reader.Read();
            }
            return c;
        }
        private Disconnector readDisconnector(XmlReader reader)
        {
            Disconnector c = new Disconnector();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Disconnector")))
            {

                if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Fuse readFuse(XmlReader reader)
        {
            Fuse c = new Fuse();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Fuse")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private GroundDisconnector readGroundDisconnector(XmlReader reader)
        {
            GroundDisconnector c = new GroundDisconnector();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:GroundDisconnector")))
            {

                if (reader.Name.Equals("cim:ScadaName") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.scadaname = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Bay readBay(XmlReader reader)
        {
            Bay c = new Bay();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();

            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Bay")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private LoadBreakSwitch readLoadBreakSwitch(XmlReader reader)
        {
            LoadBreakSwitch c = new LoadBreakSwitch();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:LoadBreakSwitch")))
            {

                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Connect_Type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.connectype = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private PowerTransformer readPowerTransformer(XmlReader reader)
        {
            PowerTransformer c = new PowerTransformer();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:PowerTransformer")))
            {

                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Serial_Number") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.SerialNumber = reader.ReadContentAsString();
                }               
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.type = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:ratedMVA") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.ratedMVA = reader.ReadContentAsString();
                }               
                else reader.Read();
            }
            return c;
        }
        private TransformerWinding readTransformerWinding(XmlReader reader)
        {
            TransformerWinding c = new TransformerWinding();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:TransformerWinding")))
            {
                if (reader.Name.Equals("cim:Naming.description") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.description = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private BusbarSection readBusbarSection(XmlReader reader)
        {
            BusbarSection c = new BusbarSection();

            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:BusbarSection")))
            {

                if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Normal_Voltage") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normolvoltage = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private ACLineSegment readACLineSegment(XmlReader reader)
        {
            ACLineSegment c = new ACLineSegment();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:ACLineSegment")))
            {   if (reader.Name.Equals("cim:Naming.description") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.description = reader.ReadContentAsString();
                }              
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Model") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.model = reader.ReadContentAsString();
                }
            else if (reader.Name.Equals("cim:LongLength") && reader.NodeType == XmlNodeType.Element)
            {
                reader.Read();
                c.length = reader.ReadContentAsString();
            }
                
                else reader.Read();
            }
            return c;
        }
        private Pole readPole(XmlReader reader)
        {
            Pole c = new Pole();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Pole")))
            {

                if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }

                else reader.Read();
            }
            return c;
        }
        private Substation readSubstation(XmlReader reader)
        {
            Substation c = new Substation();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Substation")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Breaker readBreaker(XmlReader reader)
        {
            Breaker c = new Breaker();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Breaker")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:GIS_type") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.GIStype = reader.ReadContentAsString();
                }
                else if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Circuit readCircuit(XmlReader reader)
        {
            Circuit c = new Circuit();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Circuit")))
            {
                if (reader.Name.Equals("cim:Naming.name") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.name = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        private Terminal readTerminal(XmlReader reader)
        {
            Terminal c = new Terminal();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Terminal")))
            {
                if (reader.Name.Equals("cim:Terminal.ConductingEquipment"))
                {
                    reader.MoveToAttribute("resource");
                    c.ceres = reader.ReadContentAsString();
                    c.ceres = c.ceres.Substring(1);
                    if (c.ceres.Substring(c.ceres.Length - 2, 2).Equals("_1") || c.ceres.Substring(c.ceres.Length - 2, 2).Equals("_2"))
                    {
                        c.ceres = c.ceres.Substring(0, c.ceres.Length - 2);
                    }
                }
                else if (reader.Name.Equals("cim:Terminal.ConnectivityNode"))
                {
                    reader.MoveToAttribute("resource");
                    c.cnres = reader.ReadContentAsString();
                    c.cnres = c.cnres.Substring(1);
                }
                else reader.Read();
            }
            return c;
        }
        private Switch readSwitch(XmlReader reader)
        {
            Switch c = new Switch();
            reader.MoveToAttribute("ID");
            c.id = reader.ReadContentAsString();
            while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.Equals("cim:Switch")))
            {
                if (reader.Name.Equals("cim:Switch.normalOpen") && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    c.normalopen = reader.ReadContentAsString();
                }
                else reader.Read();
            }
            return c;
        }
        /*读txt文件函数*/
        private static List<string[]> Readtxtline(string filePath)
        {
            if (File.Exists(filePath))
            {
                List<string[]> lineread = new List<string[]>();
                
                // 打开文件时 确保那个文件是GBK编码
                
                using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("GBK")))
                {
                    while (!sr.EndOfStream) //读到结尾退出
                    {
                        string temp = sr.ReadLine();
                        //将每一行拆分，分隔符就是char 数组中的字符
                        string[] strArray = temp.Split(new char[] { '\t', ' ', ',','"' }, StringSplitOptions.RemoveEmptyEntries);
                        //将拆分好的string[] 存入list                                              
                       lineread.Add(strArray);
                    }                   
                }              
                return lineread;              
            }
            return null;
        }
        private static List<string[]> Readtrans(string filePath)
        {
            if (File.Exists(filePath))
            {
                List<string[]> list = new List<string[]>();
                // 打开文件时 一定要注意编码  要GBK编码的
                using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("GBK")))
                {
                    while (!sr.EndOfStream) //读到结尾退出
                    {
                        string temp = sr.ReadLine();
                        //将每一行拆分，分隔符就是char 数组中的字符
                        string[] strArray = temp.Split(new char[] { '\t', ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                        //将拆分好的string[] 存入list
                        list.Add(strArray);
                    }
                }
                return list;
            }
            return null;
        }
        private static List<string[]> Readfuhe(string filePath)
        {
            if (File.Exists(filePath))
            {
                List<string[]> list = new List<string[]>();
                // 打开文件时 一定要注意编码 要GBK编码的
                using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("GBK")))
                {
                    while (!sr.EndOfStream) //读到结尾退出
                    {
                        string temp = sr.ReadLine();
                        //将每一行拆分，分隔符就是char 数组中的字符
                        string[] strArray = temp.Split(new char[] { '\t', ',','\"' },StringSplitOptions.RemoveEmptyEntries);
                        //将拆分好的string[] 存入list
                        list.Add(strArray);           
                    }
                    list.RemoveAt(0);
                    for (int t = 0; t < list.Count; t++)
                    {
                        for (int i = 0; i < list[t].Length; i++)
                        {
                            if (i != 0 && i != 14 && i != 15 && i != 16 && i != 18 && i != 19 && i != 20)
                            {
                                list[t][i] = "";
                            }
                        }
                        string[] k = list[t].ToList().Where(x => !x.ToString().Equals("")).ToArray();
                        list[t] = k;
                    }                                       
                }
                return list;
            }
            return null;
        }
        private static void WriteInfoTofile(string filePath, List<Tnode> list)
        {
            // 打开文件时 一定要注意编码 要GBK编码的
            using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("GBK")))
            {
                //一个string[] 是一行  ，一行中以tab键分隔
                foreach (var s in list)
                {
                    string line = string.Empty;
                    for (int i = 0; i < s.cn.Count-1; i++)
                    {
                        line += s.cn[i];
                        line += ",";
                    }
                    line += s.cn[s.cn.Count - 1];                                           
                    sw.WriteLine(line);
                }
            }
        }
   }
}   


